package com.blb.community.controller;


import com.blb.community.Annotation.Log;
import com.blb.community.entity.SysMenu;
import com.blb.community.entity.vo.MenuVo;
import com.blb.community.service.ISysMenuService;
import com.blb.community.utils.MenuTree;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@RestController
@RequestMapping("/sys-menu")
@Api(tags = "主菜单模块/菜单管理")
@CrossOrigin
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;


    /**
     * 下拉主菜单
     *
     * @param username
     * @return
     */
    @ApiOperation("主菜单")
    @GetMapping("{username}")
    public ResponseResult<List<MenuVo>> queryMenu(@PathVariable String username) {
        List<MenuVo> sysMenus = sysMenuService.queryAllMenuByUserName(username);
        return ResponseResult.ok(sysMenus);
    }

    /**
     * 菜单管理四级菜单
     *
     * @return
     */
    @ApiOperation("菜单管理")
    @GetMapping("/menu")
    public ResponseResult<List<MenuVo>> selectAllMenus(String menuName, String status) {
        List<MenuVo> menuByName = sysMenuService.selectAllMenus(menuName, status);
        //工具类树形结构菜单
        List<MenuVo> menuVos = new MenuTree(menuByName).buildTree();
        return ResponseResult.ok(menuVos);
    }

    /**
     * 添加菜单
     *
     * @param sysMenu
     * @return
     */
    @Log(module = "添加菜单",operationType =1,requestMethod = "POST")
    @ApiOperation("添加菜单")
    @PostMapping("/addMenu")
    public ResponseResult<String> addMenus(@RequestBody SysMenu sysMenu) {
        Integer integer = sysMenuService.addMenus(sysMenu, SecurityContextHolder.getContext().getAuthentication().getName());
        if (integer > 0) {
            return ResponseResult.ok("添加成功");
        }
        return ResponseResult.error("添加失败");
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return
     */
    @Log(module = "修改菜单",operationType =2,requestMethod = "PUT")
    @ApiOperation("修改菜单")
    @PutMapping("/updateMenu")
    public ResponseResult<String> updateMenu(@RequestBody SysMenu sysMenu) {
        Integer integer = sysMenuService.updateMenu(sysMenu, SecurityContextHolder.getContext().getAuthentication().getName());
        if (integer > 0) {

            return ResponseResult.ok("修改成功");
        }
        return ResponseResult.error("修改失败");
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @Log(module = "删除菜单",operationType =3,requestMethod = "DELETE")
    @ApiOperation("删除菜单")
    @DeleteMapping("/deleteMenu/{id}")
    public ResponseResult<String> deleteMenu(@PathVariable Long id) {
        Integer integer =    sysMenuService.deleteMenu(id,SecurityContextHolder.getContext().getAuthentication().getName());
        if (integer > 0) {

            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.error("删除失败");
    }
}
